package com.yh.travel.upload.controller;

import com.yh.travel.upload.utils.FileDfsUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @ClassName FileController
 * @Description 控制器
 * @Author 0715-YuHao
 * @Date 2020/11/2 10:25
 * @Version 1.0
 */
@RestController
@CrossOrigin //跨域访问
public class FileController {

    @Autowired
    private FileDfsUtil fileDfsUtil;

    @ApiOperation(value="上传文件", notes="测试FastDFS文件上传")
    @PostMapping(value = "/uploadFile",headers="content-type=multipart/form-data")
    public ResponseEntity<String> uploadFile (@RequestParam("file") MultipartFile file){
        String result ;
        try{
            String path = fileDfsUtil.upload(file) ;
            if (!StringUtils.isEmpty(path)){
                result = path ;
            } else {
                result = "上传失败" ;
            }
        } catch (Exception e){
            e.printStackTrace() ;
            result = "服务异常" ;
        }
        return ResponseEntity.ok(result);
    }

    /**
     * 文件删除
     */
    @ApiOperation(value="删除文件", notes="测试FastDFS文件删除")
    @GetMapping(value = "/deleteByPath")
    public ResponseEntity<String> deleteByPath (String filePathName){
        // String filePathName = "group1/M00/00/00/wKhjZF3WEDmAPSglAABSZAhj0eU111.jpg" ;
        fileDfsUtil.deleteFile(filePathName);
        return ResponseEntity.ok("删除成功") ;
    }
}
